<?php
namespace app\admin\controller;

use app\common\model\DrawRecord;
use rongyun\api\RongCloud;
use think\Db;



class Index extends Base
{

    function _initialize()
    {
        parent::_initialize();
        $this->assign('stamp',123);
    }

    /**
     * 获取待完成任务数据
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function getworks()
    {
        $data = $this->worksNum();
        api_return(1,'获取成功',$data);
    }


    /**
     * 获取待完成任务数据
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    protected function worksNum()
    {
        $works['protest'] = Db::name('order')->where('status',9)->count();

        $report['status'] = 0;
        $report['type']   = 1;
        $works['report_room'] = Db::name('reason')->where($report)->count();

        $report['type']   = 2;
        $works['report_user'] = Db::name('reason')->where($report)->count();

        $check['status']  = 3;
        $works['check'] = Db::name('user_id')->where($check)->count();

        $vod['type']   = 1;
        $vod['status'] = 2;
        $works['vod'] = Db::name('vod')->where($vod)->count();

        $skill['status'] = 0;
        $works['skill'] = Db::name('skill_apply')->where($skill)->count();

        $cash['type']   = 2;
        $cash['status'] = 2;
        $works['cash'] = Db::name('bankroll')->where($cash)->count();

        $works['account'] = Db::name('user_account')->where('status',2)->count();

        //修改审核数量
        $works['change'] = Db::name('check')->where('status',2)->count();

        $works['img'] = Db::name('girl_img')->where('status',2)->count();

        return $works;
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @return \think\response\View
     * 获取系统信息
     */
    public function system()
    {


        $sys_info['os']             = PHP_OS;
        $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : 'NO';//zlib
        $sys_info['safe_mode']      = (boolean) ini_get('safe_mode') ? 'YES' : 'NO';//safe_mode = Off
        $sys_info['timezone']       = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl']           = function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server']     = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv']           = phpversion();
        $sys_info['ip']             = GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') :'unknown';
        $sys_info['max_ex_time']    = @ini_get("max_execution_time").'s';
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain']         = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit']   = ini_get('memory_limit');
        $sys_info['version']        = '1.1.0';
        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']  = $mysqlinfo[0]['version'];
        if(function_exists("gd_info")){
            $gd = gd_info();
            $sys_info['gdinfo']     = $gd['GD Version'];
        }else {
            $sys_info['gdinfo']     = "δ֪";
        }

        $works = $this->worksNum();

        $this->assign([
            'sys_info' => $sys_info,
            'works' => $works,
        ]);
        return view();
    }




    public function index()
    {
        $menuList = session('menuList');
        if(empty($menuList)){
            $menuList = $this->getMenuList();
            session('menuList',$menuList);
        }

        $userInfo = Db::name('admins')->where('user_id',session('user_id'))
            ->field('nick_name,header_img,user_name,is_service')->find();


        if ($userInfo['is_service']){
            $r_token = $this->R_token(session('user_id'));
            $this->assign('r_token',$r_token);
            //图片异步上传  上传本地不需要域名前缀  上传第三方给出域名前缀
            $this->assign('domain',config('qiniu.domain'));
        }
        $this->assign('userInfo',$userInfo);
        $this->assign('menuList',$menuList);
        return view();
    }




    public function index2()
    {
//        $menuList = session('menuList');
//        if(empty($menuList)){
//            $menuList = $this->getMenuList();
//            session('menuList',$menuList);
//        }
        $menuList = $this->getMenuList();

        $model = new RongCloud(config('rongyun')['appKey'],config('rongyun')['appSecret']);
        $header_img = str_replace('\\','/','test');
        $result = $model->user()->getToken('xiaosong','xiaosong','1.png');
        $res = json_decode($result,true);
        $r_token = '';
        if ($res['code'] == 200){
            $r_token = $res['token'];
        }


        $this->assign('menuList',$menuList);
        //图片异步上传  上传本地不需要域名前缀  上传第三方给出域名前缀
        $this->assign('domain',config('qiniu.domain'));
        return view();
    }

    public function index1()
    {
        $model = new RongCloud(config('rongyun')['appKey'],config('rongyun')['appSecret']);
        $header_img = str_replace('\\','/','test');
        $result = $model->user()->getToken(hashid(2),'小莉','1.png');
        $res = json_decode($result,true);
        $r_token = '';
        if ($res['code'] == 200){
            $r_token = $res['token'];
        }
        $userInfo = Db::name('users')->where('user_id',2)->find();
        $this->assign('r_token',$r_token);
        $this->assign('userInfo',$userInfo);
        return view();
    }

    public function index3()
    {
        $model = new RongCloud(config('rongyun')['appKey'],config('rongyun')['appSecret']);
        $header_img = str_replace('\\','/','test');
        $result = $model->user()->getToken(hashid(1),'小莉','1.png');
        $res = json_decode($result,true);
        $r_token = '';
        if ($res['code'] == 200){
            $r_token = $res['token'];
        }
        $userInfo = Db::name('users')->where('user_id',1)->find();
        $this->assign('r_token',$r_token);
        $this->assign('userInfo',$userInfo);
        return view();
    }


    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 客服获取用户信息
     */
    public function getUserInfo()
    {
        $id = dehashid(input('id'));
        if (!is_numeric($id)){
            $id = 1;
        }
        $data = Db::name('users')->where('user_id',$id)->find();
        api_return(1,'获取成功',$data);
    }

    /**
     *修改密码
     */
    public function editPassword()
    {
        if (request()->isPost()){
            $data = input('post.');
            if (empty($data['old']) || empty($data['password']) || empty($data['check']))$this->error('请输入完整的数据');
            $has = Db::name('admins')->where('user_id',session('user_id'))->find();
            if ($has['password'] != md5($data['old'].'tz'))$this->error('原密码错误');
            if ($data['password'] != $data['check'])$this->error('两次密码输入不一致');
            $password = md5($data['password'].'tz');
            $result = Db::name('admins')->where('user_id',session('user_id'))->update(['password'=>$password]);
            if ($result !== false)api_return(3,'修改成功',url('login/login'));
            $this->error('修改失败');
        }
    }


    public function aaa(){
        echo substr(microtime(),2,8);
    }

    public function login(){
        return view();
    }

    public function logout(){
        session('user_id',null);
        session('menuList',null);
        $this->redirect('/login/login');
    }

    /**
     * 加密
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function hash($id)
    {
        $data = Db::name('users')->where('user_id',$id)->find();
        if ($data){
            $data['hash'] = hashid($id);
            api_return(1,'获取成功',$data);
        }
        api_return(0,'用户不存在');
    }


}
